﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TeenyGrab.Inputs;

namespace TeenyGrab.Windows
{
    /// <summary>
    /// Interaction logic for ConfigureBlankInputWindow.xaml
    /// </summary>
    public partial class ConfigureBlankInputWindow : Window
    {
        private BlankInput BlankInput { get; set; }
        private bool Initialized = false;
        public ConfigureBlankInputWindow(BlankInput blankInput)
        {
            BlankInput = blankInput;

            InitializeComponent();

            Initialized = true;

            sliderRed.Value = blankInput.Color.R;
            sliderGreen.Value = blankInput.Color.G;
            sliderBlue.Value = blankInput.Color.B;

            sliderWidth.Value = blankInput.Width;
            sliderHeight.Value = blankInput.Height;
        }

        private void buttonOk_Click(object sender, RoutedEventArgs e)
        {
            BlankInput.Color = System.Drawing.Color.FromArgb((int)Math.Round(sliderRed.Value),
                                                             (int)Math.Round(sliderGreen.Value),
                                                             (int)Math.Round(sliderBlue.Value));
            BlankInput.Width = (int)Math.Round(sliderWidth.Value);
            BlankInput.Height = (int)Math.Round(sliderHeight.Value);

            this.DialogResult = true;
            this.Close();
        }

        private void UpdateColor()
        {
            rectangle.Fill = new SolidColorBrush(Color.FromRgb((byte)Math.Round(sliderRed.Value),
                                                               (byte)Math.Round(sliderGreen.Value),
                                                               (byte)Math.Round(sliderBlue.Value)));
        }

        private void sliderRed_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!Initialized) return;
            textBoxRed.Text = "" + Math.Round(sliderRed.Value);
            UpdateColor();
        }

        private void sliderGreen_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!Initialized) return;
            textBoxGreen.Text = "" + Math.Round(sliderGreen.Value);
            UpdateColor();
        }

        private void sliderBlue_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!Initialized) return;
            textBoxBlue.Text = "" + Math.Round(sliderBlue.Value);
            UpdateColor();
        }

        private void sliderWidth_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!Initialized) return;
            textBoxWidth.Text = "" + Math.Round(sliderWidth.Value);
        }

        private void sliderHeight_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (!Initialized) return;
            textBoxHeight.Text = "" + Math.Round(sliderHeight.Value);
        }

        private void textBoxRed_TextChanged(object sender, TextChangedEventArgs e)
        {
            var red = 0;
            if (Int32.TryParse(textBoxRed.Text, out red))
            {
                sliderRed.Value = red;
            }
        }

        private void textBoxGreen_TextChanged(object sender, TextChangedEventArgs e)
        {
            var green = 0;
            if (Int32.TryParse(textBoxGreen.Text, out green))
            {
                sliderGreen.Value = green;
            }
        }

        private void textBoxBlue_TextChanged(object sender, TextChangedEventArgs e)
        {
            var blue = 0;
            if (Int32.TryParse(textBoxBlue.Text, out blue))
            {
                sliderBlue.Value = blue;
            }
        }

        private void textBoxWidth_TextChanged(object sender, TextChangedEventArgs e)
        {
            var width = 0;
            if (Int32.TryParse(textBoxWidth.Text, out width))
            {
                sliderWidth.Value = width;
            }
        }

        private void textBoxHeight_TextChanged(object sender, TextChangedEventArgs e)
        {
            var height = 0;
            if (Int32.TryParse(textBoxHeight.Text, out height))
            {
                sliderHeight.Value = height;
            }
        }
    }
}
